
## Table A.11

#directory
#set working directory to the path PvP_Replication
#setwd("~/PvP_Replication")

#packages
library(tidyverse) #for data cleaning and manipulation
library(estimatr) #estimation
library(texreg) #generate latex table 

#load main dataset
pvp_data <- read.csv("PvP_data/PvP_data_main.csv")

#transform independent variable
#create binary (hectares > 100) and log + 1 cultivation variables 
pvp_data <- pvp_data %>% 
  mutate(logcult = log(maxcult + 1))

#subset data to municipalities with prior FARC presence
pvp_farc_subset <- pvp_data %>% filter(farc_presence == 1)

#iv models

#prep weather and soil variables for iv
pvp_farc_subset <- pvp_farc_subset %>% mutate(
  draindummy = as.factor(ifelse(DRAIN == "M", 1, 0)), #moderate drainage
  humiddist = case_when(humid_mean < 85 ~ (85 - humid_mean)^2, humid_mean > 85 ~ (humid_mean - 85)^2, TRUE ~ 0 ), #distance from optimal humidity point
  temprangedist = case_when(maxtemp > 27 ~ (maxtemp - 27)^2, mintemp < 14  ~ (14 - mintemp)^2, TRUE ~ 0), #distance from optimal temp, wide band
)

#instrument for continuous independent variable
pvp_farc_subset$ols_inst <- predict(lm(logcult ~ PHAQ + temprangedist + TOTN + TOTC + sun_mean + humiddist + raintot_me + draindummy, pvp_farc_subset))

#scale variables
pvp_farc_subset <- pvp_farc_subset %>% 
  mutate(coca_log_scaled = as.numeric(scale(logcult)), 
         up_vote_scaled = as.numeric(scale(up_pres_pct_86)))

#estimate models
ols_upvote_only <- lm_robust(dissident_presence ~ up_vote_scaled, data = pvp_farc_subset)
ols_upvote_control <- lm_robust(dissident_presence ~ coca_log_scaled + up_vote_scaled, pvp_farc_subset)
iv_upvote_control <- iv_robust(dissident_presence ~ coca_log_scaled + up_vote_scaled | ols_inst + up_vote_scaled, data = pvp_farc_subset)

#generate table
texreg(
  list(ols_upvote_only, ols_upvote_control, iv_upvote_control),
  digits = 2, include.ci = FALSE, single.row = FALSE, include.fstatistic = TRUE, 
  include.rmse = FALSE, include.rsquared = FALSE, include.adjrs = FALSE, include.nobs = TRUE, 
  stars = c(0.001, 0.01, 0.05), float.pos = "h", caption.above	= TRUE, 
  custom.coef.names	= c(NA, "FARC Political Wing Voteshare", "Coca Cultivation"),
  custom.header = list("OLS" = 1, "OLS" = 2, "IV" = 3),
  caption = "Alternative FARC Support Measure: Political Wing 1986 Voteshare")
